﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmNhanVien : Form
    {
        public frmNhanVien()
        {
            InitializeComponent();
        }

        NhanvienControl control = new NhanvienControl();
        PhanquyenControl pqcontrol = new PhanquyenControl();
        bool kt = false;
        bool bInserted = false;
        public static string timnv;

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            dgvNhanVien.DataSource = control.LayDSNhanvien();
        }
        private void laythongtinnhanvien(ref NhanvienInfo info)
        {
            info.Manv = txtMaNhanVien.Text;
            info.Hoten = txtHoTen.Text;
            info.Ngaysinh = dtpNgaySinh.Value;
            info.Cmnd = txtCMND.Text;
            info.Chucvu = txtChucVu.Text;
            info.Phongban = txtPhongBan.Text;
            info.Diachi = txtDiaChi.Text;
            info.Gioitinh = cboGioiTinh.SelectedItem.ToString();
            info.Mataikhoan = txtTenTaiKhoan.Text;
            info.Dienthoai = txtDienThoai.Text;
        }
        private void laythongtinphanquyen(ref PhanquyenInfo info)
        {
            info.Matkhau = txtMatKhau.Text;
            info.Mataikhoan = txtTenTaiKhoan.Text;
            info.Quyen = 1;
        }
        private void ShowControl(bool bShow)
        {
            //txtMaNhanVien.Enabled = bInserted;
            txtChucVu.Enabled = bShow;
            txtCMND.Enabled = bShow;
            txtDiaChi.Enabled = bShow;
            txtHoTen.Enabled = bShow;
            txtDienThoai.Enabled = bShow;
            txtPhongBan.Enabled = bShow;
            cboGioiTinh.Enabled = bShow;
            dtpNgaySinh.Enabled = bShow;
            txtMatKhau.Enabled = bShow;
            txtTenTaiKhoan.Enabled = bInserted;
            btnSave.Enabled = bShow;
        }
        private void Clear()
        {
           
            txtChucVu.Text = "";
            txtCMND.Text = "";
            txtDiaChi.Text = "";
            txtHoTen.Text = "";
            txtMaNhanVien.Text = "";
            txtPhongBan.Text = "";
            txtMatKhau.Text = "";
            txtTenTaiKhoan.Text = "";
            txtDienThoai.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            int i = dgvNhanVien.Rows.Count + 1;
            txtMaNhanVien.Text = "NV" + i + "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            NhanvienInfo info = new NhanvienInfo();
            PhanquyenInfo pqinfo = new PhanquyenInfo();
            laythongtinnhanvien(ref info);
            laythongtinphanquyen(ref pqinfo);
            if (
                   txtChucVu.Text == "" ||
                   txtCMND.Text == "" ||
                   txtDiaChi.Text == "" ||
                   txtHoTen.Text == "" ||
                   
                   txtPhongBan.Text == "" ||
                   txtMatKhau.Text == "" ||
                   txtDienThoai.Text == "" ||
                   txtTenTaiKhoan.Text == ""
                   )
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (bInserted)
                {

                    if (kiemtra(txtMaNhanVien.Text.Trim()) == true)
                    {
                        MessageBox.Show("Mã Nhân Viên Bị Trùng !");
                    }
                    else
                    {
                        pqcontrol.insertphanquyenNV(pqinfo);
                        control.insertnhanvien(info);
                        MessageBox.Show("Thêm Nhân Viên Thành Công");
                        dgvNhanVien.DataSource = control.LayDSNhanvien();
                        bInserted = false;
                        ShowControl(false);
                        btnSua.Enabled = true;
                        btnThem.Enabled = true;
                    }
                }

                else
                {
                    laythongtinnhanvien(ref info);
                    laythongtinphanquyen(ref pqinfo);
                    pqcontrol.updatephanquyen(pqinfo);
                    control.updatenhanvien(info);
                    MessageBox.Show("Sửa Dữ Liệu Thành Công");
                    dgvNhanVien.DataSource = control.LayDSNhanvien();
                    ShowControl(false);
                    btnSua.Enabled = true;
                    btnThem.Enabled = true;
                }
            }
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            
        }

        private void dgvNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvNhanVien.RowCount > 0)
            {
                txtMaNhanVien.Text = dgvNhanVien.CurrentRow.Cells["MANV"].Value.ToString();
                txtHoTen.Text = dgvNhanVien.CurrentRow.Cells["HOTENNV"].Value.ToString();
                dtpNgaySinh.Text = dgvNhanVien.CurrentRow.Cells["NGSINHNV"].Value.ToString();
                txtCMND.Text = dgvNhanVien.CurrentRow.Cells["CMND_NV"].Value.ToString();
                txtChucVu.Text = dgvNhanVien.CurrentRow.Cells["CHUCVU"].Value.ToString();
                txtPhongBan.Text = dgvNhanVien.CurrentRow.Cells["PHONGBAN"].Value.ToString();
                txtDiaChi.Text = dgvNhanVien.CurrentRow.Cells["DIACHINV"].Value.ToString();
                cboGioiTinh.SelectedItem = dgvNhanVien.CurrentRow.Cells["GIOITINHNV"].Value.ToString();
                txtMatKhau.Text = dgvNhanVien.CurrentRow.Cells["MATKHAU"].Value.ToString();
                txtTenTaiKhoan.Text = dgvNhanVien.CurrentRow.Cells["MATAIKHOAN"].Value.ToString();
                txtDienThoai.Text = dgvNhanVien.CurrentRow.Cells["DIENTHOAINV"].Value.ToString();
            }
        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvNhanVien.RowCount ; j++)
            {
                if (ma == dgvNhanVien.Rows[j].Cells[0].Value.ToString())//cells[chỉ số so sánh]
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvNhanVien.RowCount > 0)
            {
                txtMaNhanVien.Text = dgvNhanVien.CurrentRow.Cells["MANV"].Value.ToString();
                txtHoTen.Text = dgvNhanVien.CurrentRow.Cells["HOTENNV"].Value.ToString();
                dtpNgaySinh.Text = dgvNhanVien.CurrentRow.Cells["NGSINHNV"].Value.ToString();
                txtCMND.Text = dgvNhanVien.CurrentRow.Cells["CMND_NV"].Value.ToString();
                txtChucVu.Text = dgvNhanVien.CurrentRow.Cells["CHUCVU"].Value.ToString();
                txtPhongBan.Text = dgvNhanVien.CurrentRow.Cells["PHONGBAN"].Value.ToString();
                txtDiaChi.Text = dgvNhanVien.CurrentRow.Cells["DIACHINV"].Value.ToString();
                cboGioiTinh.SelectedItem = dgvNhanVien.CurrentRow.Cells["GIOITINHNV"].Value.ToString();
                //txt_matkhau.Text = dgv_nhanvien.CurrentRow.Cells["MATKHAU"].Value.ToString();
                txtTenTaiKhoan.Text = dgvNhanVien.CurrentRow.Cells["MATAIKHOAN"].Value.ToString();
                txtDienThoai.Text = dgvNhanVien.CurrentRow.Cells["DIENTHOAINV"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            btnSua.Enabled = true;
            btnThem.Enabled = true;
        }

        private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            timnv = textBox3.Text;
            dgvNhanVien.DataSource = control.timnv();
        }      
      
    }
}
